#include "BasicBlock.hpp"
#include "Constant.hpp"
#include "Function.hpp"
#include "IRBuilder.hpp"
#include "Module.hpp"
#include "Type.hpp"

#include <iostream>
#include <memory>

#define CONST_INT(num) \
    ConstantInt::get(num, module)

int main() {
    auto module = new Module();
    auto builder = new IRBuilder(nullptr, module);
    auto Int32Type = module->get_int32_type();

    // 声明 callFun 函数： int callFun(int a)
    auto callFunType = FunctionType::get(Int32Type, {Int32Type});
    auto callFunFunc = Function::create(callFunType, "callFun", module);

    // 定义 callFun 函数体
    auto callFunBB = BasicBlock::create(module, "entry", callFunFunc);
    auto callFunBuilder = new IRBuilder(callFunBB, module);

    auto &paramA = *callFunFunc->get_args().begin(); // 获取参数 a（类型为 Argument&）
    auto aPlus1 = callFunBuilder->create_iadd(&paramA, CONST_INT(1)); // 显式转为 Value*
    auto retVal = callFunBuilder->create_imul(aPlus1, CONST_INT(2));  // return 2 * (a + 1)
    callFunBuilder->create_ret(retVal);

    // 定义 main 函数
    auto mainFunc = Function::create(FunctionType::get(Int32Type, {}), "main", module);
    auto mainBB = BasicBlock::create(module, "entry", mainFunc);
    builder->set_insert_point(mainBB);

    // 调用 callFun(90)
    auto call = builder->create_call(callFunFunc, {CONST_INT(90)});
    builder->create_ret(call); // return callFun(90)

    std::cout << module->print();
    delete module;
    delete builder;
    delete callFunBuilder;
    return 0;
}
